<script lang="ts" setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox, ElTable} from "element-plus";
import {customerPageService,customerAddService,customerEditService,customerDeleteService} from '@/api/basic/customer'

interface Customer {
  id: number;
  name: string; //条码
  contact: string;
  phone: string;
  email: string
}

// 列表与表单
const form = ref<Customer>({});
const tableData = ref(<Customer[]> [])
// 抽屉表单是否显示
const addDrawer = ref(false);
const updateDrawer = ref(false);

// 多选框处理
const multipleSelection = ref<Customer[]>([]) //表单引用
const handleSelectionChange = (val: Customer[]) => {  //监听表单处理函数
  multipleSelection.value = val
}

// 商品分类列表查询
const getCustomer = async ()=>{
  tableData.value = await customerPageService()
}

// 挂载函数，渲染列表
onMounted(async ()=>{
  await getCustomer()
})

// 添加
const add=()=>{
  let params = form.value
  customerAddService(params).then(()=>{
    ElMessage.success("添加成功")
    addDrawer.value = false
    form.value = {}
    getCustomer()
  })
}

// 取消添加
const addCancel=()=>{
  addDrawer.value = false
  form.value = {}
}

// 删除
const del=(row)=>{
  let deleteIds = row.id
  let params = {
    ids: deleteIds
  }
  console.log(params.ids)
  customerDeleteService(params).then(()=>{
    ElMessage.success("删除成功")
    getCustomer()
  })
}

// 批量删除
const deleteMultiple = () => {
  if (multipleSelection.value.length > 0) {
    // 用户选择了数据，显示确认对话框
    ElMessageBox.confirm(
        '是否确认删除?',
        'Warning',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }
    ).then(() => {
      // 用户确认删除，执行删除操作
      let deleteIds = multipleSelection.value.map(customer => customer.id).join(',');
      let params = {
        ids: deleteIds,
      };
      customerDeleteService(params).then(() => {
        ElMessage.success("删除成功");
        getCustomer()
      }).catch(() => {
        ElMessage.error("删除失败");
      });
    }).catch(() => {
      // 用户取消删除，不执行任何操作
      ElMessage({
        type: 'info',
        message: '取消删除',
      });
    });
  } else {
    // 未选择数据，显示错误消息
    ElMessage.error("未选择数据");
  }
};

// 修改数据回显
const updateEcho=(row)=>{
  updateDrawer.value = true
  form.value={...row}
}

// 修改
const update =()=>{
  let params = {...form.value}
  customerEditService(params).then(()=>{
    ElMessage.success("修改成功")
    updateDrawer.value = false
    getCustomer()
  })
}

// 取消修改
const updateCancel =()=>{
  updateDrawer.value = false
  form.value = {}
}

// 刷新
const refresh =()=>{
  getCustomer()
  ElMessage.success("刷新成功")
}

// 表单校验规则
const rules = ref({
  name: [
    { required: true, message: '请输入供应商名称', trigger: 'blur' },
    { min: 1, max: 20, message: '名称长度在1-20个字符之间', trigger: 'blur' }
  ],
  contact: [
    { required: true, message: '请输入联系人', trigger: 'blur' },
    { min: 1, max: 20, message: '联系人名称长度在1-20个字符之间', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
})
</script>


<template>
  <div class="button-group">
    <el-button type="primary" @click="addDrawer=true">添加客户</el-button>
    <el-button plain @click="deleteMultiple">批量删除</el-button>
    <el-button @click="refresh">刷新</el-button>
  </div>

  <!-- 表格 -->
  <div class="table-wrapper" style="width: 100%">
    <el-table height="80vh" ref="multipleTableRef" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
<!--      <el-table-column property="id"  label="ID"/>-->
      <el-table-column property="name" min-width="100" label="客户名称"/>
      <el-table-column property="contact" min-width="100" label="联系人"/>
      <el-table-column property="phone" min-width="100" label="手机号码"/>
      <el-table-column property="email" min-width="100" label="电子邮箱"/>
      <el-table-column label="操作" min-width="100">
        <template #default="row">
          <el-button link type="primary"  @click="updateEcho(row.row)" >编辑</el-button>
          <el-popconfirm title="确认删除?" @confirm='del(row.row)'>
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- 修改抽屉 -->
  <el-drawer v-model="updateDrawer" title="编辑客户" :show-close="false" >
    <el-form :model="form" :rules="rules" style="width: 80%" label-width="auto">
      <el-form-item label="名称:" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="联系人:">
        <el-input v-model="form.contact" />
      </el-form-item>
      <el-form-item label="手机号码:" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="电子邮箱:" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-button type="primary" @click="update">修改</el-button>
      <el-button @click="updateCancel">取消</el-button>
    </el-form>
  </el-drawer>

  <!-- 新增抽屉 -->
  <el-drawer v-model="addDrawer" title="添加客户" :show-close="false">
    <el-form :model="form" :rules="rules" style="width: 80%" label-width="auto">
      <el-form-item label="名称:" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="联系人:">
        <el-input v-model="form.contact" />
      </el-form-item>
      <el-form-item label="手机号码:" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="电子邮箱:" prop="email">
        <el-input v-model="form.email"/>
      </el-form-item>
      <el-button type="primary" @click="add">新增</el-button>
      <el-button @click="addCancel">取消</el-button>
    </el-form>
  </el-drawer>
</template>



<style scoped>

/* 表格盒子 */
.table-wrapper{
  margin-left: 2vh;
  margin-top: 2vh;
  //border-right: black solid 1px;
}

/* 按钮组件 */
.button-group {
  margin-left: 3vh;
  margin-top: 5px;
}

/* 按钮组件-按钮 */
.button-group .el-button {
  width: 15vh;
}
</style>